/* $Id: JDatabaseTreePaneUnitTest.java 63 2010-02-14 13:52:34Z aaron.day $ */
package org.crazydays.gameplan.db.swing;


import java.awt.Component;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.junit.*;
import static org.junit.Assert.*;

import static org.easymock.EasyMock.*;
import static org.easymock.classextension.EasyMock.*;

import org.crazydays.gameplan.db.Database;
import org.crazydays.gameplan.map.GameMap;
import org.crazydays.gameplan.model.Model;

/**
 * JDatabaseTreePaneUnitTest
 */
public class JDatabaseTreePaneUnitTest
{
    /**
     * Test initialize().
     */
    @Test
    public void testInitialize()
    {
        // mock
        Model model = createMock(Model.class);

        // setup
        JDatabaseTreePane component = new JDatabaseTreePane();
        component.setModel(model);

        // expect
        model.addDatabaseObserver(component);
        model.addGameMapObserver(component);

        // replay
        replay(model);

        // test
        component.initialize();

        // verify
        verify(model);
    }

    /**
     * Test opened(Database).
     * 
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    @Test
    public void testOpened()
        throws SecurityException, NoSuchMethodException
    {
        // mocks
        JDatabaseTreePane pane =
            createMock(JDatabaseTreePane.class, new Method[] {
                JDatabaseTreePane.class.getDeclaredMethod("loadRoot",
                    Database.class),
                JDatabaseTreePane.class.getDeclaredMethod("loadMaps",
                    Database.class),
                JDatabaseTreePane.class.getMethod("setViewportView",
                    Component.class), });
        Database database = createMock(Database.class);
        JTree tree = createMock(JTree.class);

        // expect
        pane.loadRoot(database);
        pane.loadMaps(database);
        pane.setViewportView(tree);

        // replay
        replay(pane, database, tree);

        // setup
        pane.tree = tree;

        // test
        pane.opened(database);

        // verify
        verify(pane, database, tree);
    }

    /**
     * Test loadMaps(Database).
     * 
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    @Test
    public void testLoadMaps()
        throws SecurityException, NoSuchMethodException
    {
        // objects
        Set<GameMap> gameMaps = new HashSet<GameMap>();

        // mocks
        JDatabaseTreePane pane =
            createMock(JDatabaseTreePane.class,
                new Method[] { JDatabaseTreePane.class.getMethod("added",
                    GameMap.class), });
        JTree tree = createMock(JTree.class);
        DefaultTreeModel model = createMock(DefaultTreeModel.class);
        Database database = createMock(Database.class);
        DatabaseNode root = createMock(DatabaseNode.class);
        GameMap gameMap = createMock(GameMap.class);

        // setup
        pane.tree = tree;
        pane.treeModel = model;
        pane.root = root;
        gameMaps.add(gameMap);

        // expect
        root.add(isA(MapsNode.class));
        expect(database.getGameMaps()).andReturn(gameMaps);
        pane.added(gameMap);
        tree.makeVisible(isA(TreePath.class));
        model.nodeStructureChanged(root);

        // replay
        replay(pane, tree, model, database, root, gameMap);

        // test
        pane.loadMaps(database);

        // verify
        verify(pane, tree, model, database, root, gameMap);
    }

    /**
     * Test added(IGameMap).
     */
    @Test
    public void testAdded()
    {
        // mocks
        GameMap gameMap = createMock(GameMap.class);
        MapsNode maps = createMock(MapsNode.class);
        JTree tree = createMock(JTree.class);
        DefaultTreeModel model = createMock(DefaultTreeModel.class);

        // setup
        JDatabaseTreePane pane = new JDatabaseTreePane();
        pane.maps = maps;
        pane.tree = tree;
        pane.treeModel = model;

        // expect
        expect(gameMap.getName()).andReturn("name");
        maps.add(isA(MapNode.class));
        tree.makeVisible(isA(TreePath.class));
        model.nodeStructureChanged(maps);

        // replay
        replay(gameMap, maps, tree, model);

        // test
        pane.added(gameMap);

        // verify
        verify(gameMap, maps, tree, model);
    }

    /**
     * Test removed(IGameMap).
     */
    @Test
    public void testRemoved()
    {
        // objects
        MapsNode maps = new MapsNode();
        maps.add(new MapNode(new GameMap("a")));
        maps.add(new MapNode(new GameMap("b")));
        maps.add(new MapNode(new GameMap("c")));

        // setup
        JDatabaseTreePane pane = new JDatabaseTreePane();
        pane.maps = maps;

        // test
        pane.removed(new GameMap("b"));

        // verify
        assertEquals("maps.childCount", 2, maps.getChildCount());
    }
}
